<?php
namespace app\common\service;

use app\common\util\Upload;
use app\common\util\Util;
use think\Model;
use think\Request;

class PhotoService extends BaseService
{

    /**
     * @param $request
     * @return array|bool|false|\PDOStatement|string|
     * 上传图片
     */
    public function uploadPhotoImg(Request $request){
        $sid = $request->param('sid');
        $uid = $request->param('uid');

        $user = $this->getStudentBaseInfo($sid, $uid);

        if (!$user){
            return ['status' => 0, 'msg' => '不存在此用户'];
        }


        //上传图片
        if ($request->file('img')){

            $file = $request->file('img');
            if(empty($file)){
                return ['status' => 0, 'msg' => '请选择上传图片'];
            }
            $qiniu = Upload::uploadFile();
            if($qiniu==false){
                return ['status'=> 0, 'msg'=>'上传失败', 'data' => ''];
            }else{
                $add_data=array();
                $add_data['uid']=$uid;
                $add_data['sid']=$sid;
                $add_data['addtime']=time();
                $add_data['status']=1;
                $add_data['img_url'] = Util::qiniu_url($qiniu);
                $res = db()->name('user_students_img')->insertGetId($add_data);
                if ($res === false){
                    return ['status' => 0, 'msg' => '上传失败', 'data' => ''];
                } else {
                    //上传个人照片，触发钩子
                    $studentsid = db('students')->where(['sid' => $sid, 'uid' => $uid])->value('id');
                    $param = [
                        'sid' => $sid,
                        'studentsid' => $studentsid,
                        'type' => 3,
                        'description' => '上传了照片到《个人相册》',
                    ];
                    \think\Hook::listen('students_dynamic', $param, '', true);

                    $data = [
                        'status'=> 1,
                        'data'=>[
                            'img_path' => $add_data['img_url'],
                            'img_url_small' => Util::small_qiniu_url($add_data['img_url']),
                            'id' => $res
                        ],
                        'msg'=>'上传成功'
                    ];
                    return $data;
                }
            }

        }else{
            return ['status' => 0, 'msg' => '请选择上传图片'];
        }

    }
    /**
     * @param $request
     * @return array|bool|false|\PDOStatement|string|
     * 照片列表
     */
    public function getImgList(Request $request,$size = 9)
    {
        $sid = $request->param('sid');
        $uid = $request->param('uid');

        $p = $request->param('p')?intval($request->param('p')) : 1;
        return $this->getImgListByUid($sid, $uid, $p, $size);
    }


    public function getImgListByUid($sid, $uid, $p = 1, $size = 9){
        $user = $this->getStudentBaseInfo($sid, $uid);

        if (!$user) {
            return ['status' => 0, 'msg' => '不存在此用户'];
        }
        //分页

        $start_row = ($p-1)*$size;
        $where = [
            'sid' => $sid,
            'uid' => $uid,
            'status' => 1
        ];
        $list = db()->name('user_students_img')
            ->field('id,img_url')
            ->order('addtime desc')
            ->where($where)
            ->limit($start_row , $size)
            ->select();
        if(!empty($list)){
            foreach($list as $k => $v){
                $list[$k]['img_url_small'] = Util::small_qiniu_url($v['img_url']);
            }
        }
        //总张数
        $sum = db()->name('user_students_img')
            ->where($where)
            ->count();
        $data = ['status'=>1,'data'=>['list'=>$list,'sum'=>$sum]];
        return $data;
    }


    /**
     * 微信端: 用户访问学员相册列表
     * @param $sid
     * @param $uid
     * @param $studentsid
     * @param int $p
     * @param int $pageSize
     * @return array
     */
    public function userAccessStudentsImgList($sid, $uid, $studentsid, $p = 1, $pageSize = 9){
        $students_uid = db('students')->where(['sid' => $sid, 'id' => $studentsid, 'status' => 1, 'is_counselor' => 0])->value('uid');
        if(!$students_uid){
            return ['status' => 1,'data' => ['list'=> '','sum' => 0]];
        }
        return $this->getImgListByUid($sid, $students_uid, $p, $pageSize);
    }

    /**
     * @param $request
     * @return array|bool|false|\PDOStatement|string|
     * 删除照片
     */
    public function DelImg(Request $request)
    {
        $sid = $request->param('sid');
        $uid = $request->param('uid');
        $postParam = $request->post();

        $user = $this->getStudentBaseInfo($sid, $uid);

        if (!$user) {
            return ['status' => 0, 'msg' => '不存在此用户'];
        }
        $id = $postParam['id'];
        $ids = implode(',',$id);
        $where = [
            'sid' => $sid,
            'uid' => $uid,
            'status' => 1,
            'id' => ['in',$ids]

        ];

        $res = db()->name('user_students_img')->where($where)->update(['status'=>-1]);
        if($res===false){
            $data = ['status'=>0];
        }else{
            $data = ['status'=>1];
        }

        return $data;
    }

    public function getStudentBaseInfo($sid,$uid){
        $field = 'u.user_id as uid,u.user_nickname,u.user_headimgurl,u.user_qrcode,ui.sid,ui.parent_id as counselor_id,s.name,s.phone,s.cardid,
                 s.company,s.business,s.life_motto,s.own_resource,s.needed_resource,s.homepage_style,s.homepage_bg_img';

        $where = [
            'u.status' => 1,
            'ui.userid' => $uid,
            'ui.sid' => $sid,
            'ui.students_id' => ['>',0]
        ];

        $student_info = db()->name('user_integral')
            ->alias('ui')
            ->field($field)
            ->join('students s','ui.students_id = s.id')
            ->join('user u','ui.userid = u.user_id')
            ->where($where)
            ->find();
        return $student_info;
    }

}






















